<?php
require_once("BaseDAO.php");
require_once("../Entities/Answer.php");

class AnswerDAO extends BaseDAO
{
	function CreateAnswer($answer)
	{
		$sql = "insert into Answers(Content ,QuestionId, IsCorrect, Sentence) values ('" . 
			$answer->Content . "','" . $answer->Question . "','" . $answer->IsCorrect . "','" .
			$answer->Sentence . "');";
		mysql_query($sql);
		$answer->Id = mysql_insert_id();
		return $answer;
	}
	
	function UpdateAnswer($answer)
	{
		$sql = "update Answers set Content='" . $answer->Content . "',QuestionId='" . 
			$answer->Question . "',IsCorrect='" . $answer->IsCorrect . "' where Id='" . $answer->Id . "';";
		mysql_query($sql);
	}
	
	function DeleteAnswer($id)
	{
		$sql = "delete from Answers where Id='" . $id . "';";
		mysql_query($sql);
	}
	
	function GetAnswerById($id)
	{
		$sql = "select * from Answers where Id='" . $id . "';";
		$result = mysql_query($sql);
		if(mysql_num_rows($result) == 1)
		{
			$row = mysql_fetch_assoc($result);
			$answer = new Answer();
			$answer->Id = $row["Id"];
			$answer->Content = $row["Content"];
			$answer->IsCorrect = $row["IsCorrect"];
			$answer->Question = $row["QuestionId"];
			$answer->Sentence = $row["Sentence"];
			return $answer;
		}
		return null;
	}
	
	function GetAnswersByQuestionId($id)
	{
		$sql = "select * from Answers where QuestionId='" . $id . "';";
		$result = mysql_query($sql);
		$list = array();
		while($row = mysql_fetch_assoc($result))
		{
			$answer = new Answer();
			$answer->Id = $row["Id"];
			$answer->Content = $row["Content"];
			$answer->IsCorrect = $row["IsCorrect"];
			$answer->Question = $row["QuestionId"];
			$answer->Sentence = $row["Sentence"];
			$list[] = $answer;
		}
		return $list;
	}
}

?>